define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'vppz/physical_item_rel/index' + location.search,
                    add_url: 'vppz/physical_item_rel/add?ids='+Config.package_id,
                    edit_url: 'vppz/physical_item_rel/edit',
                    del_url: 'vppz/physical_item_rel/del',
                    multi_url: 'vppz/physical_item_rel/multi',
                    table: 'vppz_package_item_rel',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url+'&ids='+Config.package_id,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),searchable:false},
                        {field: 'physical_exam.name', title: '项目名称',searchable:false},
                        {field: 'physical_exam.price', title: '项目价格',searchable:false},
                        {field: 'item_discount', title: '优惠价格',searchable:true},
                        {field: 'item_type', title:'项目类型', formatter: function(value, row, index) {
                                const genderMap = {1: '必选', 2: '默认选中', 3: '默认不选'};
                                return genderMap[value] || '必选';
                            },
                            // 筛选配置
                            searchList: {
                                '1': '必选',
                                '2': '默认选中',
                                '3': '默认不选',
                            },
                            // 筛选操作符（等于）
                            operate: '='},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime,visible: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
